package com.tigercel.xiangchengyun.ui.activities

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import cn.jzvd.JZVideoPlayer
import cn.jzvd.JZVideoPlayerStandard
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.entity.PlayListEntity
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil

import kotlinx.android.synthetic.main.include_titlebar.*
import org.jetbrains.anko.toast


class LivePlayActivity : AppCompatActivity() {
    private var type = "1"
    private var playlist1: MutableList<PlayListEntity> = ArrayList()
    private var player: JZVideoPlayerStandard? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_live_play)
        titlebar_tv.text = "广电直播"
        titlebar_iv_left.setImageDrawable(resources.getDrawable(R.drawable.btn_nav_back))
        titlebar_iv_left.visibility = View.VISIBLE
        titlebar_iv_left.setOnClickListener(View.OnClickListener { this.finish() })
        player = findViewById(R.id.videoplayer)
        getPlayList()
    }

    private fun getPlayList() {
        playlist1.clear()
        OkGo.get<String>(Global.baseUrl + "/v1/service/live/content")
                .tag(this)
                .params("type", type)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        var content = response.body()
                        var status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        if (status == "0") {
                            var playlist = JsonUtil.getList("data", content!!)
                            for (i in 0 until playlist.size) {
                                var name = JsonUtil.get_key_string("name", playlist[i])
                                var img = JsonUtil.get_key_string("thumb_img", playlist[i])
                                var url = JsonUtil.get_key_string("url", playlist[i])
                                var playlistentity = PlayListEntity(name, img, url)
                                playlist1.add(playlistentity)
                            }
                            player!!.setUp(playlist1.get(0).url, JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, "")
                            player!!.startVideo()
                            player!!.setAllControlsVisiblity(View.GONE, View.GONE, View.GONE, View.VISIBLE, View.GONE, View.GONE, View.GONE)
                        } else {
                            toast(getString(R.string.readFaile) )
                        }
                    }
                })

    }

    override fun onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return
        }
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        JZVideoPlayer.releaseAllVideos()
    }

}
